﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
using QxFramework.Utilities;
using System;

public enum EnemyName
{
    Slime,
}
public enum LevelEvent
{
    EnemyDie,
}
public class LevelManager : MonoSingleton<LevelManager>
{
    /// <summary>
    /// 波次与敌人数
    /// </summary>
    public int currentWave = 0;
    public int maxWave = 3;
    public int currentEnemy = 0;
    public int maxEnemy = 0;

    /// <summary>
    /// 怪物生成范围
    /// </summary>
    /// <param name="wave"></param>
    private float xMin = -23.77f;
    private float xMax = 26.27f;
    private float yMin = -13.88f;
    private float yMax = 14.12f;

    /// <summary>
    /// 敌人预设路径
    /// </summary>
    /// <param name="wave"></param>
    public const string ENEMY_PATH = "Prefabs/Enemy/";


    private void Start()
    {
        Init();
    }
    public void Init()
    {
        MessageManager.Instance.Get<LevelEvent>().RegisterHandler(LevelEvent.EnemyDie, EnemyDie);
        EnterWave(1);
    }

    public void EnterWave(int wave)
    {
        currentWave = wave;
        maxEnemy = wave * 1;
        currentEnemy = maxEnemy;
        for(int i = 0; i < maxEnemy; i++)
        {
            CreateEnemy(EnemyName.Slime);

        }
    }
    public void CreateEnemy(EnemyName name)
    {
        GameObject enemy = ResourceManager.Instance.Load<GameObject>(ENEMY_PATH + name.ToString());
        //GameObject go = null;
        if (enemy != null)
        {
            Instantiate<GameObject>(enemy).transform.position = new Vector3(UnityEngine.Random.Range(xMin, xMax), UnityEngine.Random.Range(yMin, yMax));
        }
        //return go;
    }


    
    public void Update()
    {
        if(currentEnemy == 0)
        {
            EnterWave(currentWave + 1);
        }
        UIManager_New.Instance.UpdateWave(currentWave);
    }

    public void EnemyDie(object sender, EventArgs args)
    {
        currentEnemy--;
    }
}
